package org.apache.shiro.session.mgt;


import javax.servlet.http.HttpServletRequest;

/**
 *  获取用户访问地址
 * <p>@author Henry.Y</p>
 * <p>@date 2019-09-10</p>
 */
public class IpUtils {

	private IpUtils(){
		
	}
	
	/**
	 * 获取用户访问地址
	 * @param request 用户请求
	 * @return 用户地址
	 */
	public static String getIpAddr(HttpServletRequest request){
		if(null == request){
			return "unknown";
		}
		String ip= request.getHeader("x-forwarded-for");
		if(null == ip || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip= request.getHeader("Proxy-Client-IP");
		}
		if(null == ip || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip= request.getHeader("X-Forwarded-For");
		}
		if(null == ip || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip= request.getHeader("WL-Proxy-Client-IP");
		}
		if(null == ip || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip= request.getHeader("X-Real-IP");
		}
		if(null == ip || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip= request.getRemoteAddr();
		}
		return ip;
	}
}
